package com.example.java_gobang.game;

import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
//游戏房间管理器
@Component
public class RoomManager {
    private ConcurrentHashMap<String,Room> roomsMap = new ConcurrentHashMap();
    //根据玩家id查找房间id
    private ConcurrentHashMap<Integer,String> userIdToRoomId = new ConcurrentHashMap<>();
    //添加房间
    public void addRoom(Room room ,int userid1,int userid2){
        roomsMap.put(room.getRoomId(),room);
        userIdToRoomId.put(userid1,room.getRoomId());
        userIdToRoomId.put(userid2,room.getRoomId());
    }
    //删除房间
    public void removeRoom(String roomId ,int userid1,int userid2){
        roomsMap.remove(roomId);
        userIdToRoomId.remove(userid1);
        userIdToRoomId.remove(userid2);
    }
    //根据房间id查找房间
    public Room getRoomById(String roomId){
        return roomsMap.get(roomId);
    }
    //根据玩家id查找房间
    public Room getRoomIdByUserId(int userId){
        String roomId = userIdToRoomId.get(userId);
        if(roomId == null){
            return null;
        }
        return roomsMap.get(roomId);
    }
}
